//
//  ZYMessageViewModel.m
//  YiLin
//
//  Created by Zyark on 2018/7/12.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYMessageViewModel.h"
#import "AFNetWorkUtils.h"
#import "ZYMessage.h"
#import "ZYLoadMoreViewModel.h"

@interface ZYMessageViewModel()

/**
 删除用户消息
 */
@property(nonatomic,strong) RACCommand *deleteMessageCommand;

/**
 加载系统消息
 */
@property(nonatomic,strong) ZYLoadMoreViewModel *loadSystemViewModel;

/**
 加载用户消息
 */
@property(nonatomic,strong) ZYLoadMoreViewModel *loadUserViewModel;

/**
 是否有新消息
 */
@property(nonatomic,strong) RACCommand *isReadCommand;

/**
 修改已读
 */
@property(nonatomic,strong) RACCommand *modifyReadCommand;
@end

@implementation ZYMessageViewModel

//上拉加载更多，下拉刷新
-(ZYLoadMoreViewModel *)loadSystemViewModel{
    if(!_loadSystemViewModel){
        _loadSystemViewModel = [[ZYLoadMoreViewModel alloc] init];
        NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_SYSTEM_MESSAGE_URL];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"5",@"pageSize",@"1",@"pageNumber",nil];
        [params setObject:[ZYConfig loginToken] forKey:@"ssid"];
        _loadSystemViewModel.clazz = [ZYMessage class];
        _loadSystemViewModel.params = params;
        _loadSystemViewModel.url = url;
        _loadSystemViewModel.sourceArray = [NSMutableArray array];
        _systemMessageCommand = _loadSystemViewModel.sourceCommand;
    }
    return _loadSystemViewModel;
}

-(ZYLoadMoreViewModel *)loadUserViewModel{
    if(!_loadUserViewModel){
        _loadUserViewModel = [[ZYLoadMoreViewModel alloc] init];
        NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_USER_MESSAGE_URL];
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"10",@"pageSize",@"1",@"pageNumber",nil];
        [params setObject:[ZYConfig loginToken] forKey:@"ssid"];
        [params setObject:[NSNumber numberWithInteger:[ZYConfig userId]] forKey:@"userId"];
        _loadUserViewModel.clazz = [ZYMessage class];
        _loadUserViewModel.params = params;
        _loadUserViewModel.url = url;
        _loadUserViewModel.sourceArray = [NSMutableArray array];
        _userMessageCommand = _loadUserViewModel.sourceCommand;
    }
    return _loadUserViewModel;
}

-(RACCommand *)userMessageCommand{
    if(!_userMessageCommand){
        _userMessageCommand = self.loadUserViewModel.sourceCommand;
    }
    return _userMessageCommand;
}

-(RACCommand *)systemMessageCommand{
    if(!_systemMessageCommand){
        _systemMessageCommand = self.loadSystemViewModel.sourceCommand;
    }
    return _systemMessageCommand;
}

-(RACCommand *)deleteMessageCommand{
    if(!_deleteMessageCommand){
        @weakify(self);
        _deleteMessageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_DELETE_MESSAGE_URL];
            NSDictionary *params = @{@"ssid":[ZYConfig loginToken],@"id":[NSNumber numberWithInteger:self.messageId]};
            NSLog(@"%@",url);
            return [AFNetWorkUtils racGETWithURL:url params:params];
        }];
    }
    return _deleteMessageCommand;
}

-(RACCommand *)modifyReadCommand{
    if(!_modifyReadCommand){
        @weakify(self);
        _modifyReadCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_MODIFY_READ_MESSAGE_URL];
            NSMutableArray *idsArray = [NSMutableArray array];
            for (ZYMessage *message in self.modifyArray) {
                if(!message.isRead){
                    [idsArray addObject:[NSNumber numberWithInteger:message.messageId]];
                }
            }
            NSString *ids = [idsArray componentsJoinedByString:@","];
            NSDictionary *params = @{@"ssid":[ZYConfig loginToken],@"ids":ids,@"isRead":@YES};
            NSLog(@"%@",url);
            NSLog(@"params:%@",params);
            return [AFNetWorkUtils racGETWithURL:url params:params];
        }];
    }
    return _modifyReadCommand;
}

-(RACCommand *)isReadCommand{
    if(!_isReadCommand){
        _isReadCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_IS_READ_MESSAGE_URL];
            NSDictionary *params = @{@"ssid":[ZYConfig loginToken],@"userId":[NSNumber numberWithInteger:[ZYConfig userId]]};
            NSLog(@"%@",url);
            return [[AFNetWorkUtils racGETWithURL:url params:params] map:^id(id value) {
                NSDictionary *result = [value objectForKey:@"result"];
                return [result objectForKey:@"isMessage"];
            }];
        }];
    }
    return _isReadCommand;
}

-(void)setupSystemParams{
    NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_SYSTEM_MESSAGE_URL];
    NSMutableDictionary *params = _loadSystemViewModel.params;
    [params setObject:[ZYConfig loginToken] forKey:@"ssid"];
    _loadSystemViewModel.params = params;
    _loadSystemViewModel.url = url;
}

-(void)setupUserParams{
    NSString *url = [NSString stringWithFormat:@"%@%@",ZYAPI_BASE_URL,ZYAPI_GET_SYSTEM_MESSAGE_URL];
    NSMutableDictionary *params = _loadSystemViewModel.params;
    [params setObject:[ZYConfig loginToken] forKey:@"ssid"];
    [params setObject:[NSNumber numberWithInteger:[ZYConfig userId]] forKey:@"userId"];
    _loadSystemViewModel.params = params;
    _loadSystemViewModel.url = url;
}

/**
 下拉刷新加载用户消息列表
 */
-(void)loadUserMessageList{
    [self setupUserParams];
    [self.loadUserViewModel.params setObject:@1 forKey:@"pageNumber"];
    [self.loadUserViewModel loadNewData];
}

/**
 上拉加载更多用户消息列表
 */
-(void)loadMoreUserMessageList{
    [self setupUserParams];
    [_loadUserViewModel.params setObject:[NSNumber numberWithInteger:_loadUserViewModel.pageNum +1] forKey:@"pageNumber"];
    [self.loadUserViewModel loadNextPageData];
}

/**
 下拉刷新加载系统消息列表
 */
-(void)loadSystemMessageList{
    [self setupSystemParams];
    [self.loadSystemViewModel.params setObject:@1 forKey:@"pageNumber"];
    [self.loadSystemViewModel loadNewData];
}

/**
 上拉加载更多系统消息列表
 */
-(void)loadMoreSystemMessageList{
    [self setupSystemParams];
    [self.loadSystemViewModel.params setObject:[NSNumber numberWithInteger:_loadSystemViewModel.pageNum +1] forKey:@"pageNumber"];
    [self.loadSystemViewModel loadNextPageData];
}

@end
